package api

import (
	"gitee.com/hexug/vblog/tree/master/api/apps/tag"
	"github.com/gin-gonic/gin"
	"io"
	"net/http"
)

func (a *TagApi) CreateTag(context *gin.Context) {
	request := tag.NewCreateTagRequest()
	err := context.ShouldBindJSON(request)
	if err != nil && err != io.EOF {
		context.JSON(http.StatusBadRequest, gin.H{
			"Code": http.StatusBadRequest,
			"Msg":  err.Error(),
		})
		context.Abort()
		return
	}
	t, err := a.Svc.CreateTag(context.Request.Context(), request)
	if err != nil {
		context.JSON(http.StatusForbidden, gin.H{
			"Code": http.StatusForbidden,
			"Msg":  err.Error(),
		})
		context.Abort()
		return
	}
	context.JSON(http.StatusOK, gin.H{
		"Code": http.StatusOK,
		"Msg":  t,
	})
}
func (a *TagApi) UpdateTag(context *gin.Context) {
	request := tag.NewUpdateTagRequest()
	err := context.ShouldBindJSON(request)
	if err != nil && err != io.EOF {
		context.JSON(http.StatusBadRequest, gin.H{
			"Code": http.StatusBadRequest,
			"Msg":  err.Error(),
		})
		context.Abort()
		return
	}
	t, err := a.Svc.UpdateTag(context.Request.Context(), request)
	if err != nil {
		context.JSON(http.StatusForbidden, gin.H{
			"Code": http.StatusForbidden,
			"Msg":  err.Error(),
		})
		context.Abort()
		return
	}
	context.JSON(http.StatusOK, gin.H{
		"Code": http.StatusOK,
		"Msg":  t,
	})
}
func (a *TagApi) DeleteTag(context *gin.Context) {
	request := tag.NewDeleteOrGetTagRequest()
	err := context.ShouldBindJSON(request)
	if err != nil && err != io.EOF {
		context.JSON(http.StatusBadRequest, gin.H{
			"Code": http.StatusBadRequest,
			"Msg":  err.Error(),
		})
		context.Abort()
		return
	}
	tags, err := a.Svc.DeleteTag(context.Request.Context(), request)
	if err != nil {
		context.JSON(http.StatusNotFound, gin.H{
			"Code": http.StatusNotFound,
			"Msg":  err.Error(),
		})
		context.Abort()
		return
	}
	context.JSON(http.StatusOK, gin.H{
		"Code": http.StatusOK,
		"Msg":  tags,
	})
}
func (a *TagApi) GetTag(context *gin.Context) {
	request := tag.NewDeleteOrGetTagRequest()
	err := context.ShouldBindJSON(request)
	if err != nil && err != io.EOF {
		context.JSON(http.StatusBadRequest, gin.H{
			"Code": http.StatusBadRequest,
			"Msg":  err.Error(),
		})
		context.Abort()
		return
	}
	tags, err := a.Svc.GetTag(context.Request.Context(), request)
	if err != nil {
		context.JSON(http.StatusNotFound, gin.H{
			"Code": http.StatusNotFound,
			"Msg":  err.Error(),
		})
		context.Abort()
		return
	}
	context.JSON(http.StatusOK, gin.H{
		"Code": http.StatusOK,
		"Msg":  tags,
	})
}
